// noinspection JSUnresolvedReference

let seckill = {
    contextPath: "",
    intervalFlag: "",
    func: {
        init: function (goodId, nowTime, startTime, endTime) {
            if (nowTime < startTime) {
                let killTime = new Date(startTime + 1000);
                $("#seckillTip").countdown(killTime, function (event) {
                    let format = event.strftime('距秒杀开始还有: %D天 %H时 %M分 %S秒');
                    $("#seckillTip").html("<span style='color:red;'>" + format + "</span>");
                }).on('finish.countdown', function () {
                    location.reload();
                });
            } else if (nowTime > endTime) {
                $("#seckillTip").html("<span style='color: red;'>来晚了，秒杀活动已经结束了~</span>");
            } else {
                seckill.func.checkIsSeckillStart(goodId);
            }
        },
        checkIsSeckillStart: function (goodId) {
            $.ajax({
                url: seckill.contextPath + "/goods/time/" + goodId,
                type: "GET",
                dataType: "json",
                success: function (responseMessage) {
                    if (responseMessage.code === 0) {
                        let random = responseMessage.data;
                        if (random) {
                            $("#seckillTip").html("<button id='seckillBtn'>立即秒杀</button>");
                            $("#seckillBtn").click(function () {
                                seckill.func.execSeckill(goodId, random);
                            })
                        }
                    }
                }
            })
        },
        execSeckill: function (goodId, random) {
            $.ajax({
                url: seckill.contextPath + "/goods/" + goodId + "/" + random,
                type: "POST",
                dataType: "json",
                success: function (responseMessage) {
                    if (responseMessage.code === 0) {
                        $("#seckillTip").html("<span style='color:blue;'>" + responseMessage.msg + "</span>")
                        seckill.intervalFlag = window.setInterval(function () {
                            seckill.func.querySeckillResult(goodId);
                        }, 3000);
                    }
                }
            })
        },
        querySeckillResult: function (goodId) {
            $.ajax({
                // http://xxx.xxx.xx/goods/result/{id}
                url: seckill.contextPath + "/goods/result/" + goodId,
                type: "GET",
                dataType: "json",
                success: function (responseMessage) {
                    if (responseMessage.code === 0) {
                        $("#seckillTip").html("<span style='color: green;'>秒杀成功，请在3分钟内完成支付，否则订单将自动关闭。<a href='https://www.alipay.com'>立即支付</a></span>");
                        window.clearInterval(seckill.intervalFlag)
                    } else if (responseMessage.code === 1) {
                        $("#seckillTip").html("<span style='color: green;'>很遗憾，秒杀失败了~</span>");
                        window.clearInterval(seckill.intervalFlag)
                    }
                }
            })
        }
    }
};